<h4><i class="fa fa-tag"></i> <?php echo Yii::t('helpdesk','View Ticket'); ?></h4><hr>
<h5><i class="fa fa-tag"></i> <?php echo Yii::t('helpdesk','Ticket'); ?> [#<?php echo $post['post_id'];?>]</h5>
<?php 
	$ticketDepartment = array();
	$ticketSubject = array();
	if(!empty($post['post_category_id'])){
		$arrPostCateIds = explode(',', $post['post_category_id']);		
		foreach($arrDepts as $D){
			if(in_array($D['category_id'], $arrPostCateIds)){
				if($D['category_level'] == 0) $ticketDepartment = $D;
				else if($D['category_level'] == 1) $ticketSubject = $D;
			}
		}
	}
?>
<table style="width:100%;" class="table table-bordered table-nonfluid table-condensed">
<tbody>
<tr>
    <td bgcolor="#fcf8e3"><?php echo Yii::t('helpdesk','Creator'); ?></td>
		<td bgcolor="#fcf8e3">
        	<?php if(!empty($post['post_creator_profile']['profile_image'])): ?>
			<img title="<?php echo $post['post_creator_profile']['profile_address']; ?>" class="avatar tt" src="<?php  echo UPLOADS_URL.'/'.$post['post_creator_profile']['profile_image']; ?>"> 
            <?php else: ?>
            <i class="fa fa-user fa-sm fa-border"></i> 
            <?php endif; ?>
            <b><?php echo $user['user_fullname']; ?></b> &lt;<?php echo $user['user_email']; ?>&gt;
        </td>
    </tr>
    <tr>
		<td><?php echo Yii::t('helpdesk','Date / Time'); ?></td>
        <td><b><?php echo date("d/m/y \n H:i:s", strtotime($post['post_created_date'])); ?></b></td>
    </tr>
    <?php if(isset($post['fields']['helpdesk-urgency'])): ?>
 	<tr>
        <td width="80px"><?php echo Yii::t('helpdesk','Urgency'); ?></td>
        <td>
			<?php switch($post['fields']['helpdesk-urgency']){
						case '1': echo '<span class="urg_1">'.Yii::t('helpdesk','Low').'</span>'; break;
						case '2': echo '<span class="urg_2">'.Yii::t('helpdesk','Medium').'</span>'; break;
						case '3': echo '<span class="urg_3">'.Yii::t('helpdesk','High').'</span>';
					}
			?>
        </td>
	</tr>
    <?php endif; ?>
	<tr>
        <td><?php echo Yii::t('helpdesk','Status'); ?></td>
        <td><?php echo ($post['post_status']=='1')?'<span class="open">'.Yii::t('helpdesk','Open').'</span>':'<span class="closed">'.Yii::t('helpdesk','Closed').'</span>'; ?></td>
	</tr>
	<tr>
		<td><?php echo Yii::t('helpdesk','Receiver'); ?></td>
        <td><?php echo !empty($ticketDepartment['category_name'])?$ticketDepartment['category_name']:null ?></td>
    </tr>
    <tr>
		<td><?php echo Yii::t('helpdesk','Subject'); ?></td>
        <td><?php echo !empty($ticketSubject['category_name'])?$ticketSubject['category_name']:null ?></td>
    </tr>     
     <tr>
        <td><?php echo Yii::t('helpdesk','Title'); ?></td>
        <td><?php echo $post['post_title']; ?></td>
	</tr>
    <tr>
        <td><?php echo Yii::t('helpdesk','Content'); ?></td>
        <td><?php echo nl2br($post['post_body']); ?></td>
	</tr>
    <?php if(isset($post['fields']['helpdesk-attachment'])): ?>
     <tr>
        <td><?php echo Yii::t('helpdesk','Attachment'); ?></td>
        <td><i class="glyphicon glyphicon-paperclip"></i> <?php echo '<a href="'.UPLOADS_URL.'/'.$post['fields']['helpdesk-attachment'].'" title="Download" target="_blank">'.pathinfo($post['fields']['helpdesk-attachment'], PATHINFO_BASENAME).'</a>' ?></td>
	</tr>
    <?php endif; ?>
    <tr>
        <td colspan="2">
        	<form id="frmActions" method="post" action="javascript:void(0);">
            	<input type="hidden" name="postId" value="<?php echo $post['post_id']; ?>">
                <input type="hidden" name="postStatus" value="<?php if(intval($post['post_status'])>0): ?>0<?php else: ?>1<?php endif; ?>">
        		<input type="submit" value="<?php if(intval($post['post_status'])>0):  echo Yii::t('helpdesk','Close ticket');  else: echo Yii::t('helpdesk','Re-open ticket'); endif; ?>" onClick="doAction('changeStatus');">
            	<?php if(intval($post['post_status'])>0): ?>
                <input type="submit" value="<?php echo Yii::t('helpdesk','Forward'); ?>" onClick="doAction('forward');">
            	<?php endif; ?>
            </form>
        </td>
	</tr>
</tbody>
</table>

<?php foreach($arrReplies as $R): ?>
<br>
<table style="width:100%;" class="table table-striped table-bordered table-nonfluid table-condensed">
<tbody>
	<tr>
    	<td>
        	<b><?php echo date("d/m/y \n H:i:s", strtotime($R['post_created_date'])); ?></b><br>
            <?php if(!empty($R['post_creator_profile']['profile_image'])): ?>
            <img class="avatar" src="<?php  echo UPLOADS_URL.'/'.$R['post_creator_profile']['profile_image']; ?>"> 
            <?php else: ?>
            <i class="fa fa-user fa-sm fa-border"></i> 
            <?php endif; ?>
			<?php echo $R['post_creator_profile']['profile_displayname']; ?> [<?php echo $R['post_creator_profile']['profile_email']; ?>]
        </td>
    </tr>
	<tr>
        <td>
			<?php echo nl2br($R['post_body']);?>
            <?php if(isset($R['fields']['helpdesk-attachment'])): ?>
        	<br>
            <i class="glyphicon glyphicon-paperclip"></i> <?php echo '<a href="'.UPLOADS_URL.'/'.$R['fields']['helpdesk-attachment'].'" title="Download" target="_blank">'.pathinfo($R['fields']['helpdesk-attachment'], PATHINFO_BASENAME).'</a>' ?>
        	<?php endif; ?> 

			<?php /* rule hiển thị: chỉ hiển thị đánh giá cho reply của người khác ĐỒNG THỜI người xem là chủ bài post  HOẶC  là nhân viên */
				  /* Rule rating: Để rate được (ko readonly) thì phải là reply chưa được rate và người rate phải là chủ bài post */
			?>
            <?php if($post['post_creator_id'] != $R['post_creator_id']): ?>
            	<?php if(  $post['post_creator_id'] == Yii::app()->user->id || Yii::app()->user->isStaff() ): ?>
                <br><br>
                <div class="ticketRating"><?php echo Yii::t('helpdesk','Rate reply'); ?>: <span class="raty" id="<?php echo $R['post_id']; ?>" score="<?php echo (float)$R['post_average_rating']; ?>" read-only="<?php echo ((float)$R['post_average_rating']>0 || ($post['post_creator_id'] != Yii::app()->user->id))?'true':'false'; ?>"></span></div>
				<?php endif; ?>
			<?php endif; ?>
        </td>
	</tr>
</tbody>
</table>
<?php endforeach; ?>
<link href="libs/jquery/raty/jquery.raty.css" type="text/css">
<script type="text/javascript" src="libs/jquery/raty/jquery.raty.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		/*raty*/
		$('.raty').raty({		
			path: 'libs/jquery/raty/images',
			click: function(score){
				var id = this.id;
				var score = score;	
				var readOnly = $(this).attr('read-only');
				if(readOnly == 'false'){
					$.ajax({
						type: "POST",
						url: "/tickets/rate",
						data: { postId: id, score: score },
						async: false
					}).done(function(data) {
						if(data != '1'){
							alert('Có lỗi xảy ra, vui lòng liên hệ admin');	
						}
					});
					$(this).raty('readOnly', true);	
				}
			},
			score: function(){
				return $(this).attr('score');
			},
			readOnly: function(){
				if($(this).attr('read-only') == 'true') return true;
				return false;
			}
		});
	});
	function doAction(act){
		if(act == 'changeStatus'){
			$('#frmActions').attr('action', '/tickets/changeStatus');	
		}
		if(act == 'forward'){
			$('#frmActions').attr('action', '/tickets/forward');
		}
	}
</script>

<?php if(intval($post['post_status'])>0): ?>
<h5><i class="glyphicon glyphicon-comment"></i>  <?php echo Yii::t('helpdesk','Reply'); ?></h5>
<form id="reply-ticket" class="form-horizontal" enctype="multipart/form-data" method="post" action="/tickets/reply">
<table style="width:100%;" class="table-bordered table-nonfluid table-condensed">
<tbody>
	<tr>
        <td>
        	<textarea style="width: 100%" name="body" rows="5" class="form-control" required></textarea>            
        </td>
	</tr>
    <tr>
        <td>
        	<input type="file" name="attach" id="attach"> <small>(<?php echo Yii::t('helpdesk','Max'); ?>: <?php echo ini_get("upload_max_filesize"); ?>) <br> <?php echo Yii::t('helpdesk','If you\'d like to upload a bunch of files please combine them into one compressed file (such as zip, rar...)'); ?></small>
        </td>
	</tr>
    <tr>
    	<td align="right">
        	<input type="hidden" name="parentPostId" value="<?php echo $post['post_id'];?>">
        	<input type="submit" value="<?php echo Yii::t('helpdesk','Send'); ?>">
        </td>
    </tr>
</tbody>
</table>
</form>
<?php endif; ?>
<?php 
//redirect
if(!isset($_SESSION['history'])) $_SESSION['history'] = '/tickets.html';
?>
<a href="<?php echo $_SESSION['history']; ?>"><i title="go back" class="fa fa-arrow-left"></i> <?php echo Yii::t('helpdesk','Go back'); ?></a>